#include "..\..\Header\Arme\canon.h"

using namespace std;

Canon::Canon()
{
    Option m_option;
    if(!m_buffertircanon.loadFromFile("sounds/tir_canon.ogg"))
    {
        cout << "impossible de charger le son du tir du canon" << endl;
    }
    else
    {
        m_soundtircanon.setBuffer(m_buffertircanon);
        m_soundtircanon.setVolume(m_option.GetVolume()/2);
    }
}
Canon::~Canon()
{

}
void Canon::SetCaracter(sf::Vector2f position,sf::Vector2f positionflip, string image, sf::IntRect subRect, Roquette roquette)
{
    m_position = position;
    L = sqrt((m_position.x*m_position.x) + (m_position.y*m_position.y));
    Lflip = sqrt((positionflip.x*positionflip.x)+(positionflip.y*positionflip.y));
    if(!m_imagecanon.loadFromFile(image))
    {
        cout << "Erreur de chargement de l'image du canon" << endl;
    }
    else
        m_spritecanon.setTexture(m_imagecanon);
    m_spritecanon.setTextureRect(subRect);
    m_spritecanon.setOrigin(0,m_spritecanon.getTextureRect().height-1);
    m_spritecanon.setScale(0.5,0.5);
    m_munition = &roquette;
    m_positiontir = sf::Vector2f(m_position.x+m_spritecanon.getTextureRect().width, m_position.y);
    m_t = acos(m_position.x/sqrt((m_position.x*m_position.x)+(m_position.y*m_position.y)));
    m_t = m_t/3.14*180;
    m_tflip = acos(positionflip.x/sqrt((positionflip.x*positionflip.x)+(positionflip.y*positionflip.y)));
    m_tflip = m_tflip/3.14*180;
    m_angle = 0;
}
void Canon::ChangerAngle(int angle, bool egal)
{
    if(egal)
        m_angle = angle;
    else
        m_angle += angle;
}
void Canon::Tir(int angle, float puissance, sf::Vector2f positiontank, sf::Vector2f vent, bool flip)
{
    m_munition->tirer(puissance, angle+m_angle,m_positiontir, vent, flip);
    m_soundtircanon.play();
}
void Canon::Rafraichir(sf::Vector2f positiontank, float angletank, bool flip)
{
    float D = m_spritecanon.getTextureRect().width;
    float t;
    float M=(m_angle+angletank)*Pi/180;
    if(flip)
    {
        t = (angletank+m_tflip)*Pi/180;
        m_position.x = positiontank.x+cos(t)*Lflip ;
        m_position.y = positiontank.y-sin(t)*L;
    }
    else
    {
        t = (angletank+m_t)*Pi/180;
        m_position.x = positiontank.x+cos(t)*L ;
        m_position.y = positiontank.y-sin(t)*L;
    }
    m_positiontir.x = cos(M)*D +m_position.x;
    m_positiontir.y = m_position.y - sin(M)*D;
    m_spritecanon.setPosition(m_position);
    m_spritecanon.setRotation(angletank+m_angle);
}
void Canon::Afficher(sf::RenderWindow *app, Terrain *terre, bool visible)
{
    if(visible)
        app->draw(m_spritecanon);

    m_munition->afficherRoquette(app);
}
void Canon::DeplacerRoquette(sf::Vector2f vent, Terrain *terre, Game *jeu, vector<sf::Sprite> unite)
{
    m_munition->deplacer(vent, terre, jeu, unite);
}
bool Canon::GetExplosion()
{
    return m_bexplosion;
}
int Canon::GetAngle()
{
    return m_angle;
}
sf::Vector2f Canon::GetRoquettePosition()
{
    return m_munition->GetPosition();
}
Canon& Canon::operator=(const Canon &canon2)
{
    m_munition = canon2.m_munition;
    m_position = canon2.m_position;
    m_positiontir = canon2.m_positiontir;
    m_bexplosion = canon2.m_bexplosion;
    m_spritecanon = canon2.m_spritecanon;
}
void Canon::Flip(bool flip)
{
    if(flip)
    {
        m_spritecanon.setScale(m_spritecanon.getScale().x, m_spritecanon.getScale().y * -1);
        //m_spritecanon.setOrigin(m_spritecanon.GetSize().x, m_spritecanon.GetCenter().y-1);
    }
    else
    {
        m_spritecanon.setScale(m_spritecanon.getScale().x, m_spritecanon.getScale().y * -1);
    }
        m_angle = 180-m_angle;
}
